var express = require('express');
var router = express.Router();
const { update,deleteImg,updatePoster,delposter} = require("../api/movies");
const { uploadFile } = require("tools-factory-tang");

// 路由作用：和页面相关的操作
router.post('/uploadMovies', async function (req, res, next) {
    // uploadFile从参1拿到的值上传到public下面，flieType的键值表示添加到具体那个文件夹里
    const { data } = await uploadFile(req, { fileType: "movies" });
    const result = await update(data);
    res.send(result);
});
// 删除图片数据
router.delete(`/:_id/imgs/:url`, async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    const { token } = req.body;
    // 获取删除后的数据
    const data = await deleteImg({ _id, url, token });
    res.send(data)
});

router.delete(`/:_id/moviesPoster/:url`, async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    const { token } = req.body;
    // 获取删除后的数据
    const data = await delposter({ _id, url, token });
    res.send(data)
});

// 上传海报
router.post('/uploadPoster', async function (req, res, next) {
    // uploadFile从参1拿到的值上传到public下面，flieType的键值表示添加到具体那个文件夹里
    const { data } = await uploadFile(req, { fileType: "moviesPoster" });
    const result = await updatePoster(data);
    res.send(result);
});


// 暴露接口
module.exports = router;